// Eternal Lands Bot
// Copyright (C) 2006  Artem Makhutov
// artem@makhutov.org
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Timers;

namespace cs_elbot.BasicCommunication
{
	/// <summary>
	/// description of HeartBeat.
	/// </summary>
	public class HeartBeat
	{
		private TCPWrapper TheTCPWrapper;
		
		// Setup Timer for the Heart_Beat
		private System.Timers.Timer HeartBeatTimer = new System.Timers.Timer();
		
		public HeartBeat(TCPWrapper MyTCPWrapper)
		{
			TheTCPWrapper = MyTCPWrapper;
			HeartBeatTimer.Interval=25000;
			HeartBeatTimer.Elapsed += new ElapsedEventHandler(HeartBeatTimer_Tick);
			
			TheTCPWrapper.GotConnected += new TCPWrapper.GotConnectedEventHandler(OnGotConnected);
			TheTCPWrapper.GotDisconnected += new TCPWrapper.GotDisconnectedEventHandler(OnGotDisconnected);
		}
		
		private void HeartBeatTimer_Tick(object sender,ElapsedEventArgs eArgs)
		{
			
			byte []	TxBuffer = new byte[3];	// Send data buffer
			TxBuffer[0]=0x0e;
			TxBuffer[1]=0x01;
			TxBuffer[2]=0x00;
			TheTCPWrapper.Send(TxBuffer);
		}
		
		private void OnGotConnected(object sender, EventArgs e)
		{
			HeartBeatTimer.Start();
		}
		
		private void OnGotDisconnected(object sender, EventArgs e)
		{
			HeartBeatTimer.Stop();
		}
	}
}
